package com.kingyun.gpsinspection.purificationservice.services.tdm.innerservice.impl;

import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.TaskLinkMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.TaskLink;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.TaskLinkService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liyi on 2016/12/22.
 *
 * 任务环节
 */
public class TaskLinkServiceImpl implements TaskLinkService {
    @Autowired
    public TaskLinkMapper taskLinkMapper;

    /**
     * 查询任务环节
     * @param taskLink
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> listMap(TaskLink taskLink) throws Exception {
        Map<String, Object> map = new HashMap<>();
        taskLink.setLimit( taskLink.getLimit() == null ? 10 : taskLink.getLimit() );
        taskLink.setOffset( taskLink.getOffset() == null ? 0 : taskLink.getOffset() );
        map.put("total",taskLinkMapper.queryCount(taskLink));
        List<TaskLink> list=taskLinkMapper.queryList(taskLink);
        map.put("rows", list);
        return map;
    }

    /**
     * 根据流程ID和NODE值查询任务环节
     * @param oldTaskLink
     * @return
     */
    @Override
    public TaskLink findByPiPiidNode(TaskLink oldTaskLink) {
        if(oldTaskLink == null){
            return null;
        }

        TaskLink taskLink = taskLinkMapper.findByPiPiidNode(oldTaskLink);

        return taskLink;
    }

    /**
     *根据流程实例和节点查询任务环节list
     * @param taskLink
     * @return
     */
    @Override
    public List<TaskLink> findListByPiPiidNode(TaskLink taskLink) {
        if(taskLink == null){
            return null;
        }

        List<TaskLink> taskLinkList = taskLinkMapper.findListByPiPiidNode(taskLink);

        return taskLinkList;
    }

    /**
     * 根据任务ID和任务环节名称查询任务环节的执行人ID
     * @param taskLink
     * @return
     */
    @Override
    public String findUserIdByTaskIdTaskLinkName(TaskLink taskLink) {
        List<TaskLink> tlList = taskLinkMapper.findListByTaskIdTaskLinkName(taskLink);

        if(tlList != null && tlList.size()>0){
            String userId = tlList.get(0).getUserId();

            return userId;
        }else{
            return "";
        }
    }
}
